#version 450 compatibility







#define M_PI 3.1415926535897
#define M_2PI 6.2831853071795


struct S_WavesInfos {
    uint    m_Frequency;    //频率
    float   m_Intensity;    //强度
    int     m_WaveOffset;   //波形偏移
    uint    m_WavesEnable;   //波形偏移
};



layout(local_size_x = DEF_LocalSize, local_size_y = DEF_LocalSize, local_size_z = 1) in;

layout( binding = 0, rgba8ui) uniform uimage2D Pix;

layout( binding = 1 ) uniform Buf_LoalParameter1 {
    ivec4   m_Box;
    ivec4   m_Offset_Size;
    uint    m_DrawType;
    uint    m_TrackNum;
    uint    m_Sampling;
};
layout( binding = 2 ) buffer Buf_LoalParameter2 {
    S_WavesInfos  m_Waves[];
};




void f_MultiSoundWaves_Mix(uint localID);



shared ivec2 coord = ivec2(0,0);

void main(){
    uint localID = 0;

    switch(m_DrawType){
    case 3:
        localID = gl_LocalInvocationID.y*gl_WorkGroupSize.x + gl_LocalInvocationID.x;

        coord.x = int(gl_WorkGroupID.x);
        coord.y = 0;
        coord += m_Box.xy;

        f_MultiSoundWaves_Mix(localID);
        break;
    }
    

}


//shared float g_sp[DEF_LocalSize*DEF_LocalSize];
//shared float currentNum[DEF_LocalSize*DEF_LocalSize];

void f_MultiSoundWaves_Mix(uint localID){
    uint local = localID * 8;
    float sp = 0.0;

    for(uint i=0; i<m_TrackNum; ++i){
        if(m_Waves[i].m_WavesEnable == 0) continue;

        float scale = float(m_Sampling) / float(m_Offset_Size.z);

        float tmp_sp = M_2PI / (float(m_Sampling) / float(m_Waves[i].m_Frequency));
        float offset = m_Waves[i].m_WaveOffset + int(gl_WorkGroupID.x);
        if(m_Offset_Size.x < 0){
            offset -= m_Offset_Size.x;
        }
        sp += sin(tmp_sp * offset * scale) * 0.1;
    }


    float height = (float(m_Offset_Size.w - 8))/2.0;
    coord.y += int(height * sp);
    coord.y += m_Offset_Size.w/2;

    if(coord.y >= m_Box.y && coord.y < m_Box.w){
        uvec4 c = uvec4(255, 255, 10, 255);
        imageStore(Pix, coord, c);
    } 

}





